package com.ruoyi.keep.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.keep.mapper.DeliveryAddressMapper;
import com.ruoyi.keep.domain.DeliveryAddress;
import com.ruoyi.keep.service.IDeliveryAddressService;

/**
 * 收货地址Service业务层处理
 * 
 * @author zqd
 * @date 2024-08-14
 */
@Service
public class DeliveryAddressServiceImpl implements IDeliveryAddressService
{
    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public DeliveryAddress selectDeliveryAddressById(Long id)
    {
        return deliveryAddressMapper.selectDeliveryAddressById(id);
    }

    /**
     * 根据用户id查询收货地址
     *
     * @param userId 收货地址主键
     * @return 收货地址
     */
    @Override
    public DeliveryAddress selectDeliveryAddressByUserId(Long userId) {
        return deliveryAddressMapper.selectDeliveryAddressByUserId(userId);
    }

    /**
     * 查询收货地址列表
     * 
     * @param deliveryAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<DeliveryAddress> selectDeliveryAddressList(DeliveryAddress deliveryAddress)
    {
        return deliveryAddressMapper.selectDeliveryAddressList(deliveryAddress);
    }

    /**
     * 根据用户di查询收货地址列表
     *
     * @param userId 收货地址
     * @return 结果
     */
    @Override
    public List<DeliveryAddress> selectDeliveryAddressListByUserId(Long userId) {
        QueryWrapper<DeliveryAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<DeliveryAddress> deliveryAddresses = deliveryAddressMapper.selectList(wrapper);
        return deliveryAddresses;
    }

    /**
     * 新增收货地址
     * 
     * @param deliveryAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertDeliveryAddress(DeliveryAddress deliveryAddress)
    {
        if (deliveryAddress.getFixed()==0) {
            DeliveryAddress deliveryAddress1 = selectByUserId(deliveryAddress.getUserId());
            if(deliveryAddress1!=null){
                deliveryAddress1.setFixed(1L);
                updateDeliveryAddress(deliveryAddress1);
            }
        }
        return deliveryAddressMapper.insertDeliveryAddress(deliveryAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param deliveryAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress)
    {
        if (deliveryAddress.getFixed()==0) {
            DeliveryAddress deliveryAddress1 = selectByUserId(deliveryAddress.getUserId());
            if(deliveryAddress1!=null){
                deliveryAddress1.setFixed(1L);
                deliveryAddressMapper.updateDeliveryAddress(deliveryAddress1);
            }
        }
        return deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryAddressByIds(Long[] ids)
    {
        return deliveryAddressMapper.deleteDeliveryAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryAddressById(Long id)
    {
        return deliveryAddressMapper.deleteDeliveryAddressById(id);
    }

    /**
     * 根据用户id查询默认收获地址
     *
     * @param UserId 用户主键
     * @return 结果
     */
    @Override
    public DeliveryAddress selectByUserId(Long UserId)
    {
        QueryWrapper<DeliveryAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",UserId).eq("fixed",0);
        DeliveryAddress deliveryAddress = deliveryAddressMapper.selectOne(wrapper);
        return deliveryAddress;
    }
}
